<?php
/******************************************************************************************************
	This class contains all the queries needed for a full install and incremental upgrades.
	This class is very similiar to the Database class, but it needs to be slightly different.
******************************************************************************************************/

class InstallAndUpgradeQueries {
  var $dbhost; //Database Host
  var $dbuser; //Database User Name
  var $dbpass; //Database Password
  var $dbase; //Database
  var $sql_query; //SQL Query
  var $mysql_link; //Resource ID for Query
  var $sql_result; //SQL Query Result
  var $connection;

  // Constructor that initializes the database abstraction layer
	function __construct() {
  }

  function InstallAndUpgradeQueries() {
  }
  
  function setDbHost($dbHost) {
    $this->dbhost = $dbHost;
  }
  
  function getDbHost() {
    return $this->dbhost;
  }
  
  function setDbUser($dbUser) {
    $this->dbuser = $dbUser;
  }
  
  function getDbUser() {
    return $this->dbuser;
  }
  
  function setDbPass($dbPass) {
    $this->dbpass = $dbPass;
  }
  
  function getDbPass() {
    return $this->dbpass;
  }
  
  function setDbase($dbase) {
    $this->dbase = $dbase;
  }
  
  function getDbase() {
    return $this->dbase;
  }

	// This function connects to the database
	// Create connection must be called before this
	function connectToDatabase() {
    $this->mysql_link = mysql_select_db($this->dbase, $this->connection);
		if (!$this->mysql_link) { //Error Detection
		   die('Could not connect: ' . mysql_error());
		}
  }
	
	// This creates a connection
	function createConnection() {
    $this->connection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass);
  }
	
	function testConnection() {
    // The @ sign tells php to hide any errors that mysql returns
    @mysql_connect($this->dbhost, $this->dbuser, $this->dbpass) or die("<h1>The database information entered is incorrect.</h1><br/>Error: " . mysql_error()."<br/>Please go back and input the correct information.");
    return true;
  }

	// This function is fed the mySQL query that is to be evaluated
	function sql_query($sql) {
		$this->sql_result = mysql_query($sql);
	}

	// Get the number of rows in the sql query
	function num_rows() {
    $mysql_rows = @mysql_num_rows( $this->sql_result );
    return $mysql_rows;
  }

	// Stores the entire mySQL query result into an array
  function get_array() {
		$i=0;
		$rowset = array();
		while ($row = $this->get_row() ) {
			$rowset[$i] = $row;
			$i++;
		}
		return $rowset;
	}

	// Just get a single row
  function get_row() {
		$row = @mysql_fetch_array($this->sql_result);
		return $row;
	}

	// Get the last auto-increment number
	function get_insert_id() {
		return mysql_insert_id();
	}

	/************************************************************************
	Determine if the database exists
	************************************************************************/
  function databaseExists() {
    $returnVal = false;
    $this->sql_result = mysql_list_dbs(mysql_connect($this->dbhost, $this->dbuser, $this->dbpass));
    while($row = $this->get_row()) {
      // We found the database!
      if($row[0] == $this->dbase) {
        $returnVal = true;
      }
    }
    return $returnVal;
  }
  
  /************************************************************************
  Get a list of all databases the user has access to.
	************************************************************************/
  function getListOfDatabases() {
    $this->sql_result = mysql_list_dbs(mysql_connect($this->dbhost, $this->dbuser, $this->dbpass));
    $array = Array();
    $i=0;
    while($row = $this->get_row()) {
      if($row[0] != "information_schema" && $row[0] != "mysql") {
        $array[$i] = $row[0];
        $i++;
      }
    }
    return $array;
  }
  
  // createConnection must be called before this!
  function createDatabase() {
    $sql = "CREATE DATABASE `".$this->dbase."` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
    
    $this->sql_query($sql);
  }
  
  /************************************************************************
  Full install of all tables
	************************************************************************/
  function fullInstall() {
    $sql = "CREATE TABLE `gban_admin_steam` (
              `admin_id` int(10) NOT NULL,
              `steam_id` varchar(255) NOT NULL,
              PRIMARY KEY  (`admin_id`),
              UNIQUE KEY `steam_id` (`steam_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
          
    $this->sql_query($sql);
    
    $sql = "CREATE TABLE `gban_admins` (
              `admin_id` int(10) NOT NULL auto_increment,
              `name` varchar(255) NOT NULL,
              `access_level` mediumint(3) NOT NULL default '1',
              `password` varchar(255) NOT NULL,
              `email` varchar(255) default NULL,
              PRIMARY KEY  (`admin_id`),
              UNIQUE KEY `name` (`name`)
            ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;";

    $this->sql_query($sql);
    
    $sql = "CREATE TABLE `gban_ban` (
              `ban_id` int(10) NOT NULL auto_increment,
              `steam_id` varchar(255) NOT NULL,
              `ip` varchar(15) default NULL,
              `name` varchar(255) default NULL,
              `length` int(10) NOT NULL default '0',
              `time_scale` VARCHAR( 12 ) NOT NULL DEFAULT 'hours',
              `add_date` datetime NOT NULL,
              `expire_date` datetime NOT NULL,
              `reason_id` int(10) NOT NULL default '1',
              `banner` varchar(255) NOT NULL,
              `banner_steam_id` varchar(255) default NULL,
              `active` tinyint(1) NOT NULL default '1',
              `pending` tinyint(1) NOT NULL default '0',
              `server_id` int(10) NOT NULL default '1',
              `webpage` varchar(255) NULL,
              `modified_by` varchar(255) NOT NULL,
              `comments` TEXT NULL,
              PRIMARY KEY  (`ban_id`),
              UNIQUE KEY `steam_id` (`steam_id`)
            ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=999 ;";
          
      $this->sql_query($sql);
      
      $sql = "CREATE TABLE `gban_demo` (
                `demo_id` int(10) NOT NULL auto_increment,
                `steam_id` varchar(255) NOT NULL,
                `demo_name` varchar(255) NOT NULL,
                `offender_name` varchar(255) NOT NULL,
                `uploader_name` varchar(255) default NULL,
                `uploader_steam_id` varchar(255) default NULL,
                `server_id` int(10) NOT NULL,
                `reason_id` int(10) NOT NULL,
                `add_date` date NOT NULL,
                `banned` tinyint(1) NOT NULL default '0',
                PRIMARY KEY  (`demo_id`),
                UNIQUE KEY `demo_name` (`demo_name`)
              ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;";
      
      $this->sql_query($sql);

      $sql = "CREATE TABLE `gban_ip` (
                `ip` varchar(15) NOT NULL,
                `active` tinyint(1) NOT NULL default '1',
                PRIMARY KEY  (`ip`)
              ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
      
      $this->sql_query($sql);

      $sql = "CREATE TABLE `gban_reason` (
                `reason_id` int(10) NOT NULL auto_increment,
                `reason` varchar(255) NOT NULL,
                PRIMARY KEY  (`reason_id`),
                UNIQUE KEY `reason` (`reason`)
              ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;";
      
      $this->sql_query($sql);

      $sql = "CREATE TABLE `gban_servers` (
                `server_id` int(10) NOT NULL auto_increment,
                `name` varchar(255) NOT NULL,
                `ip` varchar(255) NOT NULL,
                `port` int(6) NOT NULL default '27015',
                `rcon` varchar(40) NOT NULL,
                `type` varchar(40) NOT NULL default 'cstrike',
                PRIMARY KEY  (`server_id`),
                UNIQUE KEY `ip` (`ip`,`port`)
              ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;";

      $this->sql_query($sql);

      $sql = "CREATE TABLE `gban_ban_history` (
                `ban_id` int(10) NOT NULL,
                `steam_id` varchar(255) NOT NULL,
                `ip` varchar(15) default NULL,
                `name` varchar(255) default NULL,
                `length` int(10) NOT NULL default '0',
                `add_date` datetime NOT NULL,
                `expire_date` datetime NOT NULL,
                `reason_id` int(10) NOT NULL default '1',
                `banner` varchar(255) NOT NULL,
                `banner_steam_id` varchar(255) default NULL,
                `active` tinyint(1) NOT NULL default '1',
                `pending` tinyint(1) NOT NULL default '0',
                `server_id` int(10) NOT NULL default '1',
                `webpage` varchar(255) default NULL,
                `modified_by` varchar(255) NOT NULL,
                `comments` text,
                `offenses` int(3) NOT NULL default '1',
                UNIQUE KEY `ban_id` (`ban_id`),
                KEY `steam_id` (`steam_id`)
              ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";

      $this->sql_query($sql);
       
      $this->connectToDatabase();

      $sql = "INSERT INTO `gban_reason` (`reason_id`, `reason`) VALUES
              (1, 'Breaking Server Rules'),
              (2, 'Aimbot'),
              (3, 'Wallhacking'),
              (4, 'Aimbot and Wallhacking'),
              (5, 'No Recoil'),
              (6, 'Speed Hacking'),
              (7, 'Bad Behaviour (language/attitude/rude)'),
              (8, 'Mic Spamming'),
              (9, 'Nade Spamming'),
              (10, 'Offensive Spray'),
              (11, 'Advertising other servers'),
              (12, 'Clan recruitment'),
              (13, 'Ghosting'),
              (14, 'Not doing objective (Camping)'),
              (15, 'Imported Ban');";

      $this->sql_query($sql);
  }
  
  /************************************************************************
  Upgrade from Version 2.2 to 3.0
	************************************************************************/
  function upgradeTwoPointTwoToThree() {
      $sql = "ALTER TABLE `gban_ban` ADD `time_scale` VARCHAR( 12 ) NOT NULL DEFAULT 'hours' AFTER `length` ;";

      $this->sql_query($sql);
  }
}
?>
